<template>
	<view>
		<view v-if="loading">
			<view v-if="myShow">
				<view class="wrap">
					<view style="display: flex;align-items: center;justify-content: space-between;">
						<view style="width: 35%;"><u-cell-item @click="show=true"
								:title-style="{'white-space': 'nowrap','overflow': 'hidden','text-overflow': 'ellipsis'}"
								:title="govUnit.name" :arrow="true" arrow-direction="down"></u-cell-item></view>
						<view style="width: 65%;margin-left: 5rpx;">
							<u-search :show-action="false" placeholder="请输入想要查找的内容" v-model="keyword"
								@search="searchOrder()"></u-search>
						</view>
					</view>
					<view class="u-tabs-box" style="margin-bottom: 20rpx;">
						<u-tabs-swiper activeColor="#2979ff" ref="tabs" :list="list" :current="current" @change="change"
							:is-scroll="false" swiperWidth="750"></u-tabs-swiper>
					</view>
					<swiper class="swiper-box" :current="swiperCurrent" @transition="transition" @change="swiperChange"
						@animationfinish="animationfinish">
						<swiper-item class="swiper-item" style="margin-bottom: 20rpx;" v-for="(item,index) in list"
							:key="index">
							<scroll-view scroll-y style="height: 100%;width: 100%;" @scrolltolower="reachBottom">
								<view class="page-box">
									<view v-for="res in orderList[index]" :key="res.id" @click="toOrderDetail(res.id)"
										style="display: flex;align-items: center;
									justify-content: space-between;background-color: #fff;padding: 20rpx;border-radius: 16rpx;margin-bottom: 20rpx;">
										<view v-if="res.orderStatus!=sysConstant.PRE_ORDER_STATUS"
											style="width: 40%;display: flex;align-items: center;justify-content: center;flex-direction: column;">
											<view>{{res.govUnitName}}</view>
											<!-- 评审中 -->
											<view v-if="res.orderStatus==sysConstant.ING_ORDER_STATUS"
												style="display: flex;align-items: center;justify-content: center;flex-direction: column;margin-top: 10rpx;">
												<view v-if="res.dayType==-1" 
													style="height: 100rpx;width: 100rpx;border-radius: 50%;display: flex;align-items: center;justify-content: center;background-color: #fa3534;color: #fff;">
													{{res.dayNum}}天
												</view>
												<view v-if="res.dayType==1&&res.dayNum==2"
													style="height: 100rpx;width: 100rpx;border-radius: 50%;display: flex;align-items: center;justify-content: center;background-color: #ff9900;color: #fff;">
													{{res.dayNum}}天
												</view>
												<view v-if="res.dayType==1&&res.dayNum>2"
													style="height: 100rpx;width: 100rpx;border-radius: 50%;display: flex;align-items: center;justify-content: center;background-color: #19be6b;color: #fff;">
													{{res.dayNum}}天
												</view>
												<view style="color: #999;margin-top: 10rpx;">
													{{res.dayType==-1?'超时时间':'剩余时间'}}</view>
											</view>
											<!-- 平已完成 -->
											<view v-if="res.orderStatus==sysConstant.FINISH_ORDER_STATUS"
												style="display: flex;align-items: center;justify-content: center;flex-direction: column;margin-top: 10rpx;">
												<view v-if="res.dayType==1"
													style="height: 100rpx;width: 100rpx;border-radius: 50%;display: flex;align-items: center;justify-content: center;flex-direction: column;background-color: #19be6b;color: #fff;">
													<view>{{res.dayNum}}天</view>
													<!-- <view style="font-size: 24rpx;">未超时</view> -->
												</view>
												<view v-if="res.dayType==-1"
													style="height: 100rpx;width: 100rpx;border-radius: 50%;display: flex;align-items: center;justify-content: center;flex-direction: column;background-color: #fa3534;color: #fff;">
													<view>{{res.dayNum}}天</view>
													<view style="font-size: 21rpx;">(已超时)</view>
												</view>
												<view style="color: #999;margin-top: 10rpx;">实际完成时间</view>
											</view>
										</view>
										<view style="margin-left: 30rpx;" :style="{'width':(res.orderStatus!=sysConstant.PRE_ORDER_STATUS?'60%':'100%')}">
											<view style="font-size: 32rpx;">{{res.itemName}}</view>
											<view style="margin-top: 15rpx;" v-if="res.orderStatus==sysConstant.PRE_ORDER_STATUS">
												送审单位：<text style="color: #999;">{{res.govUnitName}}</text>
											</view>
											<view style="margin-top: 15rpx;">
												项目送审金额：<text style="color: #999;">{{res.submissionAmount}}万元</text>
											</view>
											<view style="margin-top: 10rpx;"
												v-if="res.orderStatus==sysConstant.PRE_ORDER_STATUS">预审开始时间：<text
													style="color: #999;">{{res.preStartTime}}</text>
											</view>
											<view style="margin-top: 10rpx;"
												v-if="res.orderStatus!=sysConstant.PRE_ORDER_STATUS">开始时间：<text
													style="color: #999;">{{res.reviewStartTime}}</text>
											</view>
											<view style="margin-top: 10rpx;"
												v-if="res.orderStatus==sysConstant.PRE_ORDER_STATUS">
												预审完成时间：<text style="color: #999;">{{res.expirationDate}}</text>
											</view>
											<view style="margin-top: 10rpx;"
												v-if="res.orderStatus==sysConstant.ING_ORDER_STATUS">
												到期时间：<text style="color: #999;">{{res.expirationDate}}</text>
											</view>
											<view style="margin-top: 10rpx;"
												v-if="res.orderStatus==sysConstant.FINISH_ORDER_STATUS">
												完成时间：<text style="color: #999;">{{res.reviewEndTime}}</text>
											</view>
										</view>
									</view>
									<view style="margin-top: 20rpx;">
										<u-loadmore :status="loadStatus[0]" bgColor="#a0cfff"></u-loadmore>
									</view>
								</view>
							</scroll-view>
						</swiper-item>
					</swiper>
				</view>

				<u-popup mode="bottom" v-model="show">
					<view class="content">
						<view
							style="display: flex;align-items: center;justify-content: space-between;margin-bottom: 20rpx;">
							<view></view>
							<view style="font-size: 32rpx;">行政单位</view>
							<u-icon color="#999" name="close-circle" size="42" @click="show = false"></u-icon>
						</view>
						<scroll-view scroll-y="true" style="max-height: 500rpx">
							<view style="background-color: #fff;border-radius: 16rpx;">
								<u-cell-item @click="selectedGov(item)" v-for="(item,index) in govUnitList" :key="index"
									:title="item.name"></u-cell-item>
							</view>
						</scroll-view>
					</view>
				</u-popup>

			</view>
			<view v-else style="margin-top: 30rpx;">
				<u-empty mode="permission"></u-empty>
			</view>
		</view>
	</view>

</template>

<script>
	export default {
		data() {
			return {
				orderList: [
					[],
					[],
					[]
				],
				list: [{
						name: '预审中'
					},
					{
						name: '评审中'
					},
					{
						name: '已完成'
					}
				],
				current: 0,
				swiperCurrent: 0,
				tabsHeight: 0,
				dx: 0,
				loadStatus: ['loadmore', 'loadmore', 'loadmore'],
				keyword: '',
				govType: '',
				govUnitList: [],
				govUnit: '',
				show: false,
				myShow: false,
				loading: false,
				keyword: '',

				orderNumList: [0, 0, 0, 0],
				pageNumber: 1,
				pageSize: 10,
				orderStatus: this.sysConstant.PRE_ORDER_STATUS,
				sysConstant: this.sysConstant
			};
		},
		onLoad() {
			uni.showLoading({

			})
			let govIdList = getApp().globalData.govIdList
			if (govIdList.length > 1 || govIdList[0] == '1875525235886776321') {
				this.loading = true
				this.myShow = true
				this.govType = '1875525235886776321'
				this.initFovUnitList()

			} else {
				this.loading = true
				this.myShow = false
			}
			uni.hideLoading()


		},
		methods: {
			reachBottom() {
				// 此tab为空数据
				if (this.orderList[this.current].length < this.orderNumList[this.current]) {
					this.loadStatus.splice(this.current, 1, "loading")
					this.pageNum += 1
					this.getOrderList(this.current);
				}
			},
			// 页面数据
			getOrderList(idx) {
				uni.showLoading({

				})
				let govgovUnitIdListStr = ""
				if (this.govUnit.id == 0) {
					let govUnitList = this.govUnitList
					let arr = []
					for (let govUnit of govUnitList) {
						arr.push(govUnit.id)
					}
					govgovUnitIdListStr = arr.join()
				} else {
					let arr = []
					arr.push(this.govUnit.id)
					govgovUnitIdListStr = arr.join()
				}
				this.$u.get("/review/order/page", {
					pageNumber: this.pageNumber,
					pageSize: this.pageSize,
					orderStatus: this.orderStatus,
					govUnitIdListStr: govgovUnitIdListStr,
					itemName: this.keyword
				}).then(res => {
					if (res.list) {
						let a = res.list
						for (let i = 0; i < a.length; i++) {
							let order = a[i]
							order.submissionAmount = order.submissionAmount.toFixed(2)
							this.orderList[idx].push(order);
						}
						this.$forceUpdate()
						console.log(this.orderList[idx])
						this.orderNumList[idx] = res.total
						uni.hideLoading()
						if (a.length == res.total) {
							this.loadStatus.splice(this.current, 1, "nomore")
						} else {
							this.loadStatus.splice(this.current, 1, "loadmore")
						}
					}
				})

			},
			//选择政府单位
			initFovUnitList() {
				this.$u.get("/gov/unit/list", {
					'govType': this.govType
				}).then(res => {
					if (res.success) {
						console.log(res)
						let govUnitList = res.data
						govUnitList.unshift({
							id: 0,
							name: '全部'
						})
						this.govUnitList = govUnitList
						this.govUnit = res.data[0]
						this.flushGetOrderList(0);
					}
				})
			},


			toOrderDetail(orderId) {
				uni.navigateTo({
					url: "/pages/order-detail/order-detail?orderId=" + orderId
				})
			},
			selectedGov(govUnit) {
				uni.showModal({
					title: '提示',
					content: '确认选择' + govUnit.name + "?",
					success: (res) => {
						if (res.confirm) {
							console.log('用户点击确定');
							this.govUnit = govUnit
							this.show = false
							this.flushGetOrderList(this.swiperCurrent);
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});

			},

			flushGetOrderList(index) {
				this.pageNumber = 1
				this.orderList[index] = []
				this.getOrderList(index)
			},
			searchOrder() {
				console.log("111111111111111")
				this.pageNumber = 1
				console.log(this.swiperCurrent)
				this.orderList[this.swiperCurrent] = []
				console.log(this.orderList)
				this.getOrderList(this.swiperCurrent)
			},
			// swiper
			swiperChange(e) {
				let index = e.detail.current
				if (index == 0) {
					this.orderStatus = this.sysConstant.PRE_ORDER_STATUS
				}
				if (index == 1) {
					this.orderStatus = this.sysConstant.ING_ORDER_STATUS
				}
				if (index == 2) {
					this.orderStatus = this.sysConstant.FINISH_ORDER_STATUS
				}
				console.log(index)
				this.flushGetOrderList(index);
			},
			// tab栏切换
			change(index) {
				this.swiperCurrent = index;
			},
			transition({
				detail: {
					dx
				}
			}) {
				this.$refs.tabs.setDx(dx);
			},
			animationfinish({
				detail: {
					current
				}
			}) {
				this.$refs.tabs.setFinishCurrent(current);
				this.swiperCurrent = current;
				this.current = current;
			}
		}
	};
</script>
<style>
	page {
		background-color: #a0cfff;
	}
</style>
<style lang="scss" scoped>
	.wrap {
		display: flex;
		flex-direction: column;
		height: calc(100vh - var(--window-top));
		width: 100%;
		padding: 0 30rpx 30rpx 30rpx;
	}

	.swiper-box {
		flex: 1;
	}

	.swiper-item {
		height: 100%;
	}

	.text-ellipsis {
		white-space: nowrap;
		/* 确保文本在一行内显示 */
		overflow: hidden;
		/* 隐藏溢出的内容 */
		text-overflow: ellipsis;
		/* 使用省略号表示文本溢出 */
	}

	.content {
		padding: 30rpx;
		text-align: center;
		background-color: #f8f8f8;
	}
</style>